/*
版权@hehung所有，转载请申明
email：1398660197@qq.com
*/
#include "app_rgb.h"
#include <drv/pin.h>
#include <drv/pwm.h>
#include "app_main.h"
#include "app_alarm.h"


static csi_pwm_t  rgb_pwm_h;
static aos_task_t rgb_task_h;    /* RGB task handle */
static uint16_t rgb_task_cycle_ms = 100;


static void Rgb_TaskEntry(void *arg);
static void Rgb_PinmuxInit(void);
static void Rgb_StopPwmChannel(uint8_t pwm_ch);
static csi_error_t Rgb_UpdatePwmDutyCycle(uint8_t pwm_ch,
										  uint16_t pwm_duty_cycle);
static void Rgb_RedLedBreath(void);
static void Rgb_RedLedLgc(void);

/* 更新LED PWM通道的占空比 */
static csi_error_t Rgb_UpdatePwmDutyCycle(uint8_t pwm_ch,
										  uint16_t pwm_duty_cycle)
{
	csi_error_t ret;
	
	csi_pwm_out_stop(&rgb_pwm_h, pwm_ch);
	
	ret = csi_pwm_out_config(&rgb_pwm_h, 
	                           pwm_ch, 
							   RGB_PWM_CYCLE_SETTING, 
							   pwm_duty_cycle, 
							   PWM_POLARITY_HIGH);
							   
    ret = csi_pwm_out_start(&rgb_pwm_h, pwm_ch);

	return ret;
}

/* 停止LED PWM通道输出 */
static void Rgb_StopPwmChannel(uint8_t pwm_ch)
{
	csi_pwm_out_stop(&rgb_pwm_h, pwm_ch);
}

/* LED相关引脚初始化 */
static void Rgb_PinmuxInit(void)
{
    csi_error_t ret;
	
    csi_pin_set_mux(PA7, PA7_PWM_CH7);    /* RED */
    csi_pin_set_mux(PA25, PA25_PWM_CH2);  /* GREEN */
    csi_pin_set_mux(PA4, PA4_PWM_CH4);    /* BLUE */
	
    ret = csi_pwm_init(&rgb_pwm_h, 0);
    if (ret != CSI_OK) {
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }

}

/* RGB函数初始化 */
void Rgb_TaskInit(void)
{
	int task_s;
	
	Rgb_PinmuxInit();
	 
	task_s = aos_task_new_ext(&rgb_task_h,
								"rgb_task", 
								Rgb_TaskEntry, 
								NULL, 
								1024, 
								3);
	if (0 == task_s)
	{
		printf("APP_RGB Task create success\r\n");
	}
	else
	{
		printf("APP_RGB Task create failed\r\n");
	}
}

/* RGB函数任务入口 */
static void Rgb_TaskEntry(void *arg)
{
    while (1) 
	{
		/* 当没有警报消息的时候，绿色呼吸灯工作，否则不工作 */
		Rgb_RedLedBreath();

		Rgb_RedLedLgc();
		
		aos_msleep(rgb_task_cycle_ms);
    }
}

/* 蓝色呼吸灯-- 标记系统正常运行 */
static void Rgb_RedLedBreath(void)
{
	static uint16_t i = RGB_PWM_CYCLE_MIN;
	static uint8_t flip_flg = 0;

	if (ALARM_WARNING_OFF == Alarm_GetalarmFlag())
	{
		if((i > RGB_PWM_CYCLE_MAX) || (i <= RGB_PWM_CYCLE_MIN))
		{
			flip_flg ^= 1;
		}
		i = (1 == flip_flg) ? (i + RGB_PWM_CHANGE_STEP) : (i - RGB_PWM_CHANGE_STEP);
	}
	else
	{
		i = 300;
	}
	
	/* Green LED 呼吸灯，表示程序运行正常 */
	Rgb_UpdatePwmDutyCycle(RGB_GREEN_PWM_CH, i);	
}

/* 红色警报灯 - 闹钟警报 */
static void Rgb_RedLedLgc(void)
{
	static uint16_t duty_cycle = 300;    /* 默认处于熄灭状态 */
	static uint8_t flip_flg = 0;

	/* 闹钟激活了 */
	if (ALARM_WARNING_ON == Alarm_GetalarmFlag())
	{
		if ((duty_cycle <= 0) || (duty_cycle >= 300))
		{	
			flip_flg ^= 1;
		}

		duty_cycle = (flip_flg == 1) ? (duty_cycle - 30) : (duty_cycle + 30);
	}
	else
	{
		duty_cycle = 300;
	}

	Rgb_UpdatePwmDutyCycle(RGB_RED_PWM_CH, duty_cycle);
}

/* 点亮LED */
uint8_t Rgb_LedOn(uint8_t num, uint16_t dutycycle)
{
	uint8_t err = ERR_OK;

	if (((RGB_GREEN_PWM_CH == num) || (RGB_RED_PWM_CH == num) ||
	     (RGB_BLUE_PWM_CH == num)) &&
		 (dutycycle < RGB_PWM_CYCLE_SETTING))
	{
		Rgb_UpdatePwmDutyCycle(num, dutycycle);
	}
	else
	{
		err = ERR_NOT_OK;
	}

	return err;
}
